<template>
  <!-- 工程建设标准规范清单 -->
  <base-breadcrumb class="allPlan">
    <searchData :searchData="searchArr" @search="search"></searchData>
    <a-card>
      <!-- 表格 -->
      <list-table :page-keys="['pageNum', 'pageSize', 'total']" ref="table" :columns="columns" :request="loadData">
        <!-- 新增 -->
        <a-button v-btnPermission="'tm_016_add_01'" slot="prefix" icon="plus" type="primary" @click="handleManage(2)"
          >新增</a-button
        >
        <template slot="linkUrl" slot-scope="text">
          <a :href="text" target="_blank">{{text}}</a>
        </template>

        <template slot="index" slot-scope="text,record,index">
          {{index+1}}
        </template>
        <span slot="operation" slot-scope="text, record">
          <MoreButton>
          <a v-btnPermission="'tm_016_loo_04'" @click="handleManage(0, record)">查看</a>
          <!-- 编辑 -->
          <template
            v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED' || record.auditStatus === 'PAST'"
          >
            <a @click="handleManage(record.auditStatus === 'PAST' ? -1 : 1, record)"  v-btnPermission="'tm_016_edi_02'">编辑</a>
          </template>
            <a @click="setVisibleLink(record)" v-btn-permission="'tm_016_con_07'">配置参考规范链接</a>
          <template v-if="record.auditStatus == 'REJECTED' || record.auditStatus == 'DRAFT'">
              <!-- 删除 -->
              <a
                style="color: #1890ff;"
                v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'"
                v-btnPermission="'tm_016_del_03'"
                @click="handleRemoveParams({ id: record.id, type: 1 })"
              >
                删除
              </a>
              <!-- 发起审批 -->
              <a
                style="color: #1890ff;"
                v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'"
                v-btnPermission="'tm_016_del_03'"
                @click="launchProcessReq({ id: record.id })"
              >
                发起审批
              </a>
              <!-- 审批 -->
              <a v-if="record.auditStatus !== 'DRAFT'" v-btnPermission="'tm_016_loo_04'">
                <approval-button :id="record.id" :auditStatus="record.auditStatus" /> </a>
          </template>
          <template v-else>
            <!-- 删除 -->
        
            <!-- 审批 -->
            <a v-if="record.auditStatus !== 'DRAFT'" v-btnPermission="'tm_016_loo_04'">
              <approval-button :id="record.id" :status="record.auditStatus" />
            </a>
          </template>
            </MoreButton>
        </span>

        <span slot="auditStatus" slot-scope="text, record">
          <list-status :status="record.auditStatus" :isApproval="true" conversion="en" :text="text"></list-status>
        </span>
      </list-table>
    </a-card>
    <selectApprover :businessId="businessId"  :processConfig="setProcessConfig" :isCloseUpper="false" @close="close"></selectApprover>
    <a-modal v-model:visible="visibleLink" title="配置参考链接" @ok="handleOk" okText="提交">
      <a-form-model :model="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 14 }">
        <a-form-model-item label="配置参考链接" class="requiredFormItem">
          <a-input v-model="form.referenceLink" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </base-breadcrumb>
</template>

<script>
import BasePage from '@/views/basePage' // 基础配置
import { getBasicsStandardList, delBasicsStandard, launchProcess } from '@/api/technical/specificationList'
import  { technicalBasicsStandardUpdateReferenceLink } from "@/api/technology"
import TreeProject from '@/components/businessComponents/TreeProject' // 树
import MoreButton from '@/components/MoreButton'
import PermissionTreeOrganization from '@/components/businessComponents/PermissionTreeOrganization'
import { status } from '../model.js'
const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'index' }
  },
  {
    title: '年份',
    dataIndex: 'year',
    ellipsis: true
  },

  {
    title: '发布日期',
    dataIndex: 'releaseDate',
    ellipsis: true
  },
  {
    title: '参考规范链接',
    dataIndex: 'referenceLink',
    scopedSlots: { customRender: 'linkUrl' }
  },
  {
    title: '状态',
    dataIndex: 'auditStatus',
    scopedSlots: { customRender: 'auditStatus' }
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    width: '180px',
    scopedSlots: { customRender: 'operation' }
  }
]

import { STable } from '@/components'
export default {
  name: 'a' + Date.now(),
  extends: new BasePage(),
  components: {
    STable,
    TreeProject,
    MoreButton,
    PermissionTreeOrganization
  },
  data() {
    this.columns = columns

    return {
      visibleLink: false,
      queryParam: {},

      removeApi: { remove: delBasicsStandard }, // api
      loadData: async parameter => {
        const requestParameters = Object.assign({}, parameter, { param: this.queryParam })

        return getBasicsStandardList(requestParameters).then(res => {
          if (res.code === '0') {
            res.data.list.forEach(i => {
              i.auditStatus = status[i.auditStatus]
            })
            return {
              code: 200,
              data: res.data.list,
              total: res.data?.total,
              currentPage: res.data?.pageNum
            }
          }
        })
      },
      searchArr: [
        {
          name: '年份',
          type: 'year',
          value: 'year',
          yearsOpen: false,
          attrs: {
            valueFormat: 'YYYY'
          }
        }
      ],
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null,
      form: {
        referenceLink: ''
      },
      selectedRowKeys: []
    }
  },
 async mounted(){
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('technical_basics_standard_audit')   
 },
  methods: {
    handleOk() {
      if(!this.form.referenceLink) {
        this.$message.warning('请填写完所有必填项信息')
        return
      }
      if(this.form.referenceLink.indexOf('http://') === -1 && this.form.referenceLink.indexOf('https://')=== -1) {
        this.$message.warning('请填写正确的链接')
        return
      }
      const data = {
        id: this.form.id,
        referenceLink: this.form.referenceLink
      }
      technicalBasicsStandardUpdateReferenceLink(data).then(res=>{
        this.$message.success('提交成功')
        this.visibleLink = false;
        this.search()
      })
    },
    setVisibleLink(row) {
      this.form = { ...this.form, ...row };
      this.visibleLink = true;
    },
    // 发起审批
    launchProcessReq(param) {
      this.setProcessConfig = null;   
      this.businessId = null; 
       this.submitApprovalConfirm(this.processConfig, async (flag)=>{
          return launchProcess(param).then((res) => {
            this.$message.success('发起成功')
            //提交时，返回id
              this.businessId = res.data.id;
              this.setProcessConfig = this.processConfig;
            this.$refs.table.loadData()
          }) 
       })
    },
    /**
     * 删除草稿:api 请求api;params 请求参数 批量删除
     */
    handleRemoves(ids) {
      this.$confirm({
        title: '温馨提示',
        content: '确定删除？',
        onOk: () => {
          return this.removeApi
            ?.remove({
              ids: ids
            })
            .then(() => {
              this.$message.success('删除成功！')
              this.$refs.table.loadData()
            })
        }
      })
    },
    //搜索
    search(value) {
      value && (this.queryParam = value)
      this.$refs.table.refresh(true)
    },
    close(){
     this.$refs.table.refresh(true)
    },
    // 操作栏
    handleManage(type, record) {
      let query = {
        editModel: type == -1 ? 1 : type
      }
      if (record && record.id) {
        query.id = record.id
      }
      this.$router.push({
        name: 'updateBaseStandard',
        query
      })
    }
  }
}
</script>
